<template>
  <el-dialog v-if="load" :close-on-click-modal="false" :title="isLook ? '冲账详情' : '申请冲账'" v-model="visible" align-center :z-index="2000"
    width="1300px" @close="handleClose">
    <el-form ref="formRef" :model="form" label-width="110px" :disabled="isLook">
      <div class="v-title mb-3">基础信息</div>
      <div class="grid grid-cols-4 gap-x-3">
        <el-form-item label="供应商">
          <el-input v-model="form.basic.supplier_name" placeholder="请输入" disabled />
        </el-form-item>
        <el-form-item label="供应商类型">
          <VSelect v-model="form.basic.purchase_type" placeholder="请输入" :options="options.purchase_type || []" filterable
            clearable disabled />
        </el-form-item>
        <el-form-item label="结算方式">
          <el-input v-model="form.basic.period_name" placeholder="请输入" disabled />
        </el-form-item>
        <el-form-item label="公司主体">
          <VSelect v-model="form.basic.company_id" placeholder="请输入" :options="options.company_id || []" filterable
            clearable disabled />
        </el-form-item>
        <el-form-item label="交易方式">
          <VSelect v-model="form.basic.payment_type" placeholder="请输入" :options="options.payment_type || []" filterable
            clearable disabled />
        </el-form-item>
        <el-form-item label="支付账号">
          <VSelect v-model="form.basic.payment_account" placeholder="请输入" :options="options.alipay_account || []"
            filterable clearable disabled />
        </el-form-item>
        <el-form-item label="支付币种">
          <VSelect v-model="form.basic.currency" placeholder="请输入" :options="options.currency || []" filterable clearable
            disabled />
        </el-form-item>
        <el-form-item label="收款人">
          <el-input v-model="form.basic.collect_name" placeholder="收款人" class="w-full" disabled />
        </el-form-item>
        <el-form-item label="收款账号">
          <el-input v-model="form.basic.collect_account" placeholder="请输入" class="w-full" disabled />
        </el-form-item>
        <el-form-item label="银行名称">
          <el-input v-model="form.basic.collect_bank" placeholder="请输入" disabled />
        </el-form-item>
        <el-form-item label="采购人员">
          <el-input v-model="form.basic.purchase_user_name" placeholder="请输入" disabled />
        </el-form-item>
      </div>
      <div class="v-title mb-3">采购单信息</div>
      <vxe-table ref="tableRef" :data="form.list" size="small">
        <vxe-column title="采购单号" :min-width="140">
          <template #default="{ row }">
            <el-link type='primary'>{{ row.purchase_no }}</el-link>
          </template>
        </vxe-column>
        <vxe-column title="下单时间" field="platform_order_time" :min-width="160" />
        <vxe-column title="下单数量" field="purchase_num" :min-width="80" />
        <vxe-column title="收货数量" field="deliver_num" :min-width="80" />
        <vxe-column title="入库数量" field="inbound_num" :min-width="80" />
        <vxe-column title="不良品数" field="reject_num" :min-width="80" />
        <vxe-column title="退货数量" field="return_num" :min-width="80" />
        <vxe-column title="下单总金额" field="purchase_num" :min-width="90">
          <template #default="{ row }">
            {{ row.is_drawback === 1 ? row.total_purchase_price : row.total_purchase_price_tax }}
          </template>
        </vxe-column>
        <vxe-column title="入库总金额" field="inbound_price" :min-width="90" />
        <vxe-column title="退款总金额" field="refund_price" :min-width="90" />
        <vxe-column title="已付总金额" field="payment_price" :min-width="90" />
        <vxe-column title="入库时间" field="inbound_time" :min-width="160" />
        <vxe-column title="入库仓" field="inbound_warehouse" :min-width="80" />
        <vxe-column title="可冲账金额" field="strike_price" :min-width="90" />
        <vxe-column title="调整金额" :min-width="120">
          <template #default="{ row }">
            <el-input-number v-model="row.adjustment_price" :controls="false" :min="0" :step="0.01" :precision="2"
              class="w-full" />
          </template>
        </vxe-column>
        <vxe-column title="调整备注" :min-width="120">
          <template #default="{ row }">
            <el-input v-model="row.remark" placeholder="请输入" type="textarea"
              :autosize="{ minRows: 2, maxRows: 2 }" />
          </template>
        </vxe-column>
      </vxe-table>
    </el-form>
    <template v-if="isLook" #footer>
      <el-button @click="close">关 闭</el-button>
    </template>
    <template v-else #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loadings.submit" @click="create()">提 交</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from '../api'
import { ElMessage } from 'element-plus'
const props = defineProps({
  options: { type: Object, default: () => ({}) },
})
const emit = defineEmits(['close', 'success'])

const load = ref(false)
const visible = ref(false)
let loadings = reactive({})

// 表单处理
const formRef = ref()
const form = ref({})

// 打开弹窗
const isLook = ref(false)
const open = async (params, look) => {
  isLook.value = look
  if (!load.value) {
    load.value = true
    await 1
  }
  form.value = {
    basic: {},
    list: [],
  }
  loadings = reactive({})
  formRef?.value?.resetFields()

  try {
    const res = await api[params.p_ids ? 'strike' : 'detail'](params)
    form.value = res.data
    if (!form.value.list.length) {
      ElMessage.error('无有效数据')
      return
    }
  } catch (error) {
    return
  }
  visible.value = true

  form.value.basic.return_type = 1
  form.value.basic.purchase_user_name = form.value.list[0].purchase_user_name
  form.value.list.forEach(d => {
    d.apply_refund_price = d.can_refund_price
  })
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const create = async () => {
  if (!await formRef?.value.validate()) return
  const purchase_list = form.value.list.map(d => {
    const { p_id, adjustment_price, remark } = d
    return { p_id, adjustment_price, remark }
  })
  loadings.submit = true
  api.addStrike({ purchase_list }).then(res => {
    ElMessage.success('操作成功')
    emit('success')
    close()
  }).finally(() => {
    loadings.submit = false
  })
}

defineExpose({ open })
</script>